// hover 框文本样式
function DrawUtil(zr,grp){
    var that = this;
    that.zr = zr;
    that.grp = grp;
    that.txt = null;
    $(document).on('click',function(){
        that.txt.hide();
    })
}

DrawUtil.prototype = {
    // 初始化悬浮文本
    initHoverText: function(){
        var that = this;
        that.txt = new zrender.Text({
            style: {
                fontSize: 14,
                textBackgroundColor: '#FFF',
                textBorderColor: '#000',
                textBorderWidth: 1,
                textBorderRadius: 2,
                textPadding: 5
            },
            zlevel: 100
        });
        that.zr.add(that.txt);
        that.txt.hide();
        return that.txt;
    },
    showText: function(p){
        var that = this;
        var show = function(){
            that.txt.show();
            that.txt.attr('position', [p.x, p.y]);
            that.txt.attr({
                style: {
                    text: p.txt
                }
            })
        };
        var res;
        if (res){clearTimeout(res)}
        res = setTimeout(show,20);
    },
    hText: function(p){
        var that = this;
       var txt = new zrender.Text({
           style:{
               text: p.str,
               fontSize: p.fontSize || 14,
               textFill: p.color || '#000',
               fontWeight: p.fontWeight
           }
       });
       txt.attr('position',[p.dx, p.dy]);
       that.grp.add(txt);
       return txt;
    },
    // 绘制纵向文本
    vText: function(p) {
        var that = this;
        var group = new zrender.Group();
        for (var i = 0; i < p.str.length; i++) {
            var txt = new zrender.Text({
                style: {
                    text: p.str.charAt(i),
                    fontSize: p.fontsize || 14,
                    textFill: p.textFill || '#FF4949',
                    textBackgroundColor: p.backgroundColor || '#FFF',
                }
            });
            var y = 14 * i;
            txt.attr('position', [0, y]);
            group.add(txt)
        }

        group.attr('position', [p.dx, p.dy]);
        that.grp.add(group);
        return group
    },
    // 绘制实心圆
    circel: function(p) {
        var that = this;
        var c = new zrender.Circle({
            shape: {
                cx: p.x,
                cy: p.y,
                r: 4
            },
            style: {
                fill: p.color || '#FAD'
            }
        }).on('mouseover', function () {
            that.showText(p);
        }).on('mouseout', function () {
            //that.txt.hide()
        });
        that.grp.add(c);
        return c;
    },
    // 绘制三角形
    isogon:function(p) {
        var that = this;
        var s = new zrender.Isogon({
            shape: {
                x: p.x,
                y: p.y,
                r: 6,
                n: 3
            },
            style: {
                fill: p.color || '#ADF'
            }
        }).on('mouseover', function () {
            that.showText(p);
        }).on('mouseout', function () {
            //that.txt.hide()
        });
        that.grp.add(s);
        return s;
    },
    // 绘制空心圆
    hollowCircle:function(p) {
        var that = this;
        var c = new zrender.Circle({
            shape: {
                cx: p.x,
                cy: p.y,
                r: 4
            },
            style: {
                fill: '#FFFFFF',
                stroke: p.color || '#DAF'
            }
        }).on('mouseover', function () {
            that.showText(p);
        }).on('mouseout', function () {
            that.txt.hide()
        });
        that.grp.add(c);
        return c;
    },
    // 绘制×
    drawX: function(p) {
        var that = this;
        var txt = new zrender.Text({
            style: {
                text: '×',
                fontSize: 18,
                textFill: p.color || '#FAD',
            },
            position: [p.x - 7, p.y - 8]
        }).on('mouseover', function () {
            that.showText(p);
        }).on('mouseout', function () {
            //that.txt.hide()
        });
        that.grp.add(txt);
        return txt;
    },
    // 绘制圆圈中有点的圆
    pointCircle:function(p) {
        var that = this;
        var group = new zrender.Group();
        var Point = new zrender.Circle({
            shape: {
                cx: 4,
                cy: 4,
                r: 1
            },
            style: {
                fill: p.color || '#0000'
            }
        });
        var OutCircle = new zrender.Circle({
            shape: {
                cx: 4,
                cy: 4,
                r: 4
            },
            style: {
                fill: '#FFFFFF',
                stroke: p.color
            }
        });
        group.on('mouseover', function () {
            that.showText(p);
        }).on('mouseout', function () {
            //that.txt.hide()
        });

        group.add(OutCircle);
        group.add(Point);
        group.attr('position', [p.x - 3, p.y - 3]);

        that.grp.add(group);
        return group;
    },
    // 绘制圆圈中有H的圆
    hCircle:function(p) {
        var that = this;
        var group = new zrender.Group();
        var h = new zrender.Text({
            style: {
                text: 'H',
                fontSize: 8,
                textFill: p.color || '#000'
            },
            position: [2, -1]
        });
        var OutCircle = new zrender.Circle({
            shape: {
                cx: 5,
                cy: 5,
                r: 5
            },
            style: {
                fill: '#FFFFFF',
                stroke: p.color || '#000'
            }
        });
        group.on('mouseover', function () {
            that.showText(p);
        }).on('mouseout', function () {
            //that.txt.hide()
        });

        group.add(OutCircle);
        group.add(h);
        group.attr('position', [p.x - 4, p.y - 4]);

        that.grp.add(group);
        return group;
    },
    // 绘制圆圈中有加号的圆
    plusCircle:function(p) {
        var that = this;
        var group = new zrender.Group();
        var h = new zrender.Text({
            style: {
                text: '+',
                fontSize: 11,
                textFill: p.color || '#000'
            },
            position: [1, -1]
        });
        var OutCircle = new zrender.Circle({
            shape: {
                cx: 5,
                cy: 5,
                r: 5
            },
            style: {
                fill: '#FFFFFF',
                stroke: p.color || '#000'
            }
        });
        group.on('mouseover', function () {
            that.showText(p);
        }).on('mouseout', function () {
            //that.txt.hide();
        });

        group.add(OutCircle);
        group.add(h);
        group.attr('position', [p.x - 4, p.y - 4]);

        that.grp.add(group);
        return group;
    },
    path: function(p){
        var that = this;
        var p = zrender.path.createFromString(p.str,{
            style:{
                fill: p.fill || 'none',
                stroke: p.stroke || 'black',
                lineWidth: p.lineWidth || 1
            }
        });
        that.grp.add(p);
        return p;
    },
    polyLine: function(p) {
        var that = this;
        var c = new zrender.Polyline({
            shape: {
                points: p.points
            },
            style: {
                fill: 'none',
                stroke: p.color || '#000'
            }
        });
        that.grp.add(c);
        return c;
    }

};